import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Card} from 'antd';
import {PERMISSION} from './constants';
import WrappedSelect from '../common/WrappedSelect';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import Export from '../common/Export';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import {injectIntl, language} from './intl';
import {partClaimType, partClaimFormStatus} from '../Enum';
import DateRangePicker from 'Shared/components/DateRangePicker';
import TextInput from 'Shared/components/TextInput';
import TagSelect from 'Shared/components/TagSelect';
const FormItem = Form.Item;
const partClaimTypeList = partClaimType.toList(language);
const partClaimFormStatusList = partClaimFormStatus.toList(language);
class QueryPanel extends React.PureComponent {
    state = {
        data: {},
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.handleFilterChange(data.id, 'dealerId');
        this.props.onSelect({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {brands} = this.props.initEnumData;
        const condition = this.props.conditions || {};
        const dealerInfo = condition.dealerId ? {
            id: this.props.queryDealer.id,
            code: this.props.queryDealer.code,
            name: this.props.queryDealer.name
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            {brands.length !== 1 && <Col {...FORM_OPTIONS.col}><FormItem label={formatMessage({
                                id: 'queryPanel.form.brandId',
                                defaultMessage: '品牌'
                            })}{...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="brandId"
                                    value={this.props.conditions.brandId}
                                    options={brands}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                            </Col>}
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.dealer',
                                    defaultMessage: '总代'
                                })} {...FORM_OPTIONS.item}>
                                    <DealerSelectPanel
                                        value={dealerInfo}
                                        onSelect={this.onSelectDealer} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.invoiceCode',
                                        defaultMessage: '发票号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="invoiceCode"
                                        value={this.props.conditions.invoiceCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.code',
                                        defaultMessage: '备件索赔单编号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="code"
                                        value={this.props.conditions.code}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.partCode',
                                        defaultMessage: '零件号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="partCode"
                                        value={this.props.conditions.partCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.claimType',
                                        defaultMessage: '索赔类型'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="claimType"
                                        value={this.props.conditions.claimType}
                                        options={partClaimTypeList}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.submitTime',
                                        defaultMessage: '提交时间'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="submitTime"
                                        value={this.props.conditions.submitTime}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col>
                                <FormItem label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })} {...FORM_ROW_OPTIONS.item}>
                                    <TagSelect
                                        name="status"
                                        value={this.props.conditions.status}
                                        options={partClaimFormStatusList}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}</Button>
                                <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}</Button>
                            </Col>
                            <Col span={18} className="col-align-right">
                                {this.props.exportable && <Export key={PERMISSION.export} exportRequest={this.props.onClickExport} />}
                            </Col>
                        </Row>
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, saveQueryDealer, exportData} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    queryDealer: getQueryDealer(state),
    exportable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.export),
    detailExport: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.detailExport),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(saveQueryDealer(data)),
    onClickExport: () => dispatch(exportData()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
